import { Body, Controller, Headers, Post, Req } from '@nestjs/common';
import { FastifyRequest } from 'fastify';
import { LoginService } from './login.service';
import { Authorize } from '../core/decorators/authorize.decorator';
import { LogDisabled } from '../core/decorators/log-disabled.decorator';
import { ApiOkResponse, ApiOperation, ApiTags } from '@nestjs/swagger';
import { UtilService } from '@main/shared/services/util.service';
import { LoginInfoDto } from './login.dto';
import { LoginToken } from './login.class';

@ApiTags('登录模块')
@Controller()
export class LoginController {
  constructor(private loginService: LoginService, private utils: UtilService) {}

  @ApiOperation({
    summary: '管理员登录',
  })
  @ApiOkResponse({ type: LoginToken })
  @Post('login')
  @LogDisabled()
  @Authorize()
  public async login(
    @Body() dto: LoginInfoDto,
    @Req() req: FastifyRequest,
    @Headers('user-agent') ua: string
  ) {
    // const { userName,password } = data;

    // if (userName === 'admin' && password === 'admin123') {
    //   return IpcSuccessException({
    //     userInfo: {
    //       userName: 'admin',
    //       nickName: '超级管理员',

    //     }
    //   });

    // }

    const token = await this.loginService.getLoginSign(
      dto.userName,
      dto.password,
      this.utils.getReqIP(req),
      ua
    );

    return token;

    // webContents.send('reply-msg', 'this is msg from webContents.send');

    // return userInfo;
  }
}
